package com.swan.controllers;

import javafx.animation.RotateTransition;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.Group;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.util.Duration;

import java.net.URL;
import java.util.Random;
import java.util.ResourceBundle;

/**
 * HelloController
 * @author xyh
 * @param <T>
 * @date 20230426
 *
 */

public class HelloController implements Initializable {

    private double angleTurned = 0.0;

    @FXML
    private Group spinnerPolygon;
    @FXML
    private Label label;
    @FXML
    private Button button;
    Random r = new Random();
    @FXML
    private void handleButtonAction(ActionEvent event) {
        RotateTransition spinSpinner;
        spinSpinner = new RotateTransition(Duration.millis(1000), spinnerPolygon);
        double randomNum = angleToSpin();
        angleTurned = (angleTurned + randomNum) % 360.0;
        spinSpinner.setByAngle(randomNum);
        spinSpinner.play();
        int newLoc = landinglocation(angleTurned);

        spinSpinner.setOnFinished(event1 -> label.setText("you are on quartile: " + newLoc));


    }

    private double angleToSpin() {

        return (r.nextDouble() * 720);
    }

    private int landinglocation(double d) {
        return (int) Math.floor((360.0 - d) / 90);
    }

    @Override
    public void initialize(URL url, ResourceBundle rb) {
        // TODO
    }

}
